package com.work.service.main.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.work.constant.ExceptionDescription;
import com.work.dto.transport.TransportSearchDTO;
import com.work.entity.main.TShipBill;
import com.work.entity.main.TShipForecast;
import com.work.mapper.main.TShipBillMapper;
import com.work.service.main.ITShipBillService;
import com.work.service.transport.ITransportService;
import com.work.vo.transport.TransportBillVO;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 理货舱单接口实现
 *
 * @author ：dujianyu
 * @since ：Created in 2024/3/12 15:07
 */
@Service
@AllArgsConstructor
public class TShipBillServiceImpl extends BaseServiceImpl<TShipBillMapper, TShipBill> implements ITShipBillService {

    private ITransportService transportService;

    @Override
    public Long addTransportBill(Long transportShipId, Long transportBillId) {
        Long existTshipBillId = isTransportBillExist(transportBillId);
        if (existTshipBillId != (long) -1) {
            return existTshipBillId;
        }

        TransportSearchDTO transportSearchDTO = new TransportSearchDTO();
        transportSearchDTO.setTransportBillId(transportBillId);
        transportSearchDTO.setTransportShipId(transportShipId);
        List<TransportBillVO> transportBills = transportService.selectTransportBillList(transportSearchDTO);

        if (Func.isNull(transportBills) || Func.isEmpty(transportBills)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "货运系统无该舱单信息");
        }

        TShipBill entity = BeanUtil.copy(transportBills.get(0), TShipBill.class);
        if (Func.isNull(entity) || !super.saveOrUpdate(entity)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "向理货添加舱单信息失败");
        }
        return entity.getId();

    }

    @Override
    public TShipBill getDetailByTransportBillId(Long transportBillId) {
        QueryWrapper<TShipBill> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TShipBill::getIsDeleted, 0)
                .eq(TShipBill::getTransportBillId, transportBillId)
                .last("limit 1");
        TShipBill res = new TShipBill();
        TShipBill tShipBill = baseMapper.selectOne(queryWrapper);
        if (!Func.isNull(tShipBill) && !Func.isNull(tShipBill.getId())) {
            res = tShipBill;
        }
        return res;
    }

    private Long isTransportBillExist(Long transportBillId) {
        QueryWrapper<TShipBill> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(TShipBill::getId)
                .eq(TShipBill::getIsDeleted, 0)
                .eq(TShipBill::getTransportBillId, transportBillId)
                .last("limit 1");
        TShipBill tShipBill = baseMapper.selectOne(queryWrapper);
        if (Func.isNull(tShipBill)) {
            return (long) -1;
        } else {
            return tShipBill.getId();
        }
    }
}
